package com.li.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.li.constant.MessageConstant;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.entity.Result;
import com.li.pojo.CheckItem;
import com.li.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    CheckItemService checkeItemService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem, HttpServletRequest request) {
        request.setAttribute("message", "");
        checkeItemService.addCheckItem(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);

    }

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request) {
        request.setAttribute("message", MessageConstant.QUERY_CHECKITEM_FAIL);
        PageResult pageResult = checkeItemService.selectPageByCondition(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/delete")
    //系统管理员才能进行检查项的删除
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result delete(Integer id, HttpServletRequest request) {
        request.setAttribute("message", "");
        checkeItemService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);

    }

    @RequestMapping("findById")
    public Result findById(Integer id, HttpServletRequest request) {
        request.setAttribute("message", "");
        CheckItem result = checkeItemService.findById(id);
        Result res = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
        res.setData(result);
        return res;
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem, HttpServletRequest request) {
        request.setAttribute("message", "");
        //对数据进行更新
        checkeItemService.edit(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(HttpServletRequest request) {
        request.setAttribute("message", "");
        List<CheckItem> all = checkeItemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, all);
    }
}
